<?php
// 课程安排
namespace Home\Controller;

use Think\Controller;

class CurriculumController extends Controller
{

    public function _initialize()
    {
        if (empty($_SESSION['empid'])) {

            $this->error('页面加载中。。。', U('Login/Login'), 1);
        }
    }

    public function Daochu()
    {
        $sql = session('ssql');

        $fs = I('get.fs');
        R('Excel/Out', array(
            $sql,
            $fs
        ));
    }

    public function Classes()
    {
        $Classes = D('CSchedule');
        $param = I('param.');
        unset($param['p']);
        if ($param) {
            //var_dump(1);
            $scheid = $param['scheid'];
            $schename = $param['schename'];
            $classlb = $param['classlb'];
            session('schename', null);
            session('schename', null);
            session('scheid', null);
            session('zbdw', null);
            $zbdw = $param['zbdw'];
            if (!empty($scheid)) {
                $map['scheid'] = array(
                    'like',
                    '%' . $scheid . '%'
                );
                session('scheid', $scheid);
            }
            if (!empty($schename)) {
                $map['schename'] = array(
                    'like',
                    "%" . $schename . "%"
                );
                session('schename', $schename);
            }
            if (!empty($classlb)) {
                $map['classlb'] = array(
                    'like',
                    "%" . $classlb . "%"
                );
                session('classlb', $classlb);
            }
            if (!empty($zbdw)) {

                session('zbdw', $zbdw);
                $map['zbdw'] = array(
                    'like',
                    '%' . $zbdw . '%'
                );
            }

            $count = $Classes->where($map)->count(); // 查询满足要求的总记录数

            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)

            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('last', '共%TOTAL_PAGE%页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $Classes->where($map)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->Relation(true)
                ->select();

            $ssql = $Classes->where($map)
                ->fetchSql(true)
                ->select();
            session('ssql', $ssql);
        } else {
            var_dump(2);
            $count = $Classes->count(); // 查询满足要求的总记录数

            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('last', '共%TOTAL_PAGE%页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $Classes->limit($Page->firstRow . ',' . $Page->listRows)
                ->Relation(true)
                ->select();
            $ssql = $Classes->fetchSql(true)->select();
            session('ssql', $ssql);
        }

        $this->assign('page', $show); // 赋值分页输出
        // echo $ssql;
        // var_dump($tinfo);
        $this->assign('tinfo', $tinfo);
        $dqlist = R('Pub/Classlb');
        $this->assign('dqlist', $dqlist);
        $this->display(); // 输出模板
    }

    public function AddClasses()
    {
        if (IS_AJAX) {
            $map['scheid'] = I('post.scheid');
            $sum = M('CSchedule')->where($map)->count();
            if ($sum > 0) {

                $data = 3;
            } else {
                $Classes = D('CSchedule');
                $Classes->create();
                $add = $Classes->add();
                if ($add) {

                    $data = 0;
                } else {
                    $data = 1;
                }
            }

            echo $data;
        } else {
            $bcbh = R('Dr/creatbc');
            $telist = R('Pub/Teacher');
            $kclist = R('Pub/Kecheng');
            $dqlist = R('Pub/Classlb');
            $kc = R('Pub/Kc');

            $this->assign('kc', $kc);
            $this->assign('bcbh', $bcbh);
            $this->assign('telist', $telist);
            $this->assign('kclist', $kclist);
            $this->assign('dqlist', $dqlist);
            $this->display();

        }
    }

    public function Classesxg()
    {
        $Classes = D('CSchedule');

        if (IS_AJAX) {
            $Classes->create();
            $updata = $Classes->save(); // 根据条件保存修改的数据
            if ($updata) {

                $data = 0;
            } else {

                $data = 1;
            }
            echo $data;
        } else {
            $telist = R('Pub/Teacher');
            $kclist = R('Pub/Kecheng');
            $dqlist = R('Pub/Classlb');
            $this->assign('telist', $telist);
            $this->assign('kclist', $kclist);
            $this->assign('dqlist', $dqlist);
            $kc = R('Pub/Kc');
            $this->assign('kc', $kc);
            $map['id'] = I('get.id');
            $us = $Classes->where($map)->find();
            $this->assign('us', $us);
            $this->display();
        }
    }

    public function Currinfo()
    {
        $Classes = D('CCourse');
        $param = I('param.');
        unset($param['p']);
        if ($param) {
            $courseid = $param['courseid'];
            $coursename = $param['coursename'];
            $teacher = $param['teacher'];
            $class = $param['class'];
            $start_time = $param['start_time'];
            $end_time = $param['end_time'];
            session('courseid', null);
            session('coursename', null);
            session('teacher', null);
            session('class', null);
            session('start_time', null);
            session('end_time', null);
//            $coursename = I('post.coursename');
//            $teacher = I('post.teacher');
//            $class = I('post.class');
//            $start_time = I('post.start_time');
//            $end_time = I('post.end_time');
//            $scheid = I('post.scheid');
            if (!empty($courseid)) {
                session('courseid', $courseid);

                $map['courseid'] = array(
                    'eq',
                    $courseid
                );
            }

            if (!empty($coursename)) {
                session('coursename', $coursename);
                $map['coursename'] = array(
                    'like',
                    '%' . $coursename . '%'
                );
            }
            if (!empty($teacher)) {
                session('teacher', $teacher);
                $map['teacher'] = array(
                    'eq',
                    $teacher
                );
            }
            if (!empty($class)) {
                session('class', $class);
                $map['class'] = array(
                    'eq',
                    $class
                );
            }
            if (!empty($start_time) && !empty($end_time)) {
                session('start_time', $start_time);
                session('end_time', $end_time);
                if (strtotime($start_time) > strtotime($end_time)) {

                    $this->error('时间区间选择错误');
                }
                $map['start_time'] = array(
                    'BETWEEN',
                    '' . $start_time . ',' . $end_time . ''
                );
            }
            $count = $Classes->where($map)->count(); // 查询满足要求的总记录数

            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('last', '共%TOTAL_PAGE%页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $Classes->where($map)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->order('courseid desc')
                ->select();

            $ssql = $Classes->where($map)
                ->field("id,courseid,coursename,start_time,teacher,lesson,class,number,money,remark,crc,
                case when is_am=1 then '上午' when is_am=2 then '下午' when is_am=3 then '晚上' end as is_am")
                ->fetchSql(true)
                ->select();

            session('ssql', $ssql);
        } else {
            $count = $Classes->count(); // 查询满足要求的总记录数

            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('last', '共%TOTAL_PAGE%页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $Classes->limit($Page->firstRow . ',' . $Page->listRows)
                ->order('courseid desc')
                ->select();
            $ssql = $Classes->field("id,courseid,coursename,start_time,teacher,lesson,class,number,money,remark,crc,
                case when is_am=1 then '上午' when is_am=2 then '下午' when is_am=3 then '晚上' end as is_am")
                ->fetchSql(true)->select();
            session('ssql', $ssql);
        }

        $this->assign('page', $show); // 赋值分页输出
        $telist = R('Pub/Teacher');
        $classs = R('Pub/Classs');
        // dump($stul);
        $this->assign('telist', $telist);
        $this->assign('classs', $classs);
        $bcbh = M('CSchedule')->field('scheid', 'schename')->select();
        $this->assign('bcbh', $bcbh);
        $this->assign('tinfo', $tinfo);
        $this->display(); // 输出模板
    }

    public function Addcurr()
    {
        if (IS_AJAX) {
            $data['is_am'] = I('post.is_am');
            $data['coursename'] = I('post.coursename');
            $data['start_time'] = I('post.start_time');
            $data['teacher'] = I('post.teacher');
            $data['number'] = I('post.number');
            if (empty($_POST['crc'])) {

                $data['crc'] = "无";
            } else {
                $data['crc'] = I('post.crc');
            }
            $data['class'] = I('post.class');
            $data['courseid'] = I('post.courseid');
            $add = M('CCourse')->add($data);
            if ($add) {
                $data = 0;
            } else {
                $data = 1;
            }
            echo $data;
        } else {
            if (!empty($_POST['bh'])) {

                $bh = I('post.bh');
            } elseif (!empty($_GET['bh'])) {

                $bh = I('get.bh');
            }
            if ($bh == '') {

                $this->error('班次不存在');
            }

            if (I('get.sd') != '') {
                if (I('get.fs') == 'down') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 + (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'up') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 - (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'now') {
                    $sd = strtotime('last monday');
                }
            } else {
                $sd = strtotime('last monday');
            }
            $this->assign('sd', $sd);
            $d = $sd;

            for ($i = 0; $i < 7; $i++) {

                $riqi = date('Y-m-d', $d);
                $star_time = date('Y-m-d', $d);

                $map['start_time'] = $star_time;

                $map['courseid'] = $bh;

                $sq[$riqi] = M('CCourse')->where($map)->select();

                $d += 86400;
            }
            $kc = R('Pub/Kc');
            $teacher = R('Pub/Teacher');
            $this->assign('kc', $kc);
            $this->assign('teacher', $teacher);
            $class = R('Pub/Classs');
            $this->assign('class', $class);

            $this->assign('sq', $sq);
            $this->assign('riqi', $riqi);
            // var_dump($sq);
            $this->assign('bh', $bh);
            $mn['scheid'] = $bh;
            $ccname = M('CSchedule')->where($mn)->getfield('schename');
            $this->assign('ccname', $ccname);
            $this->assign('bh', $bh);
            $this->display();
        }
    }

    public function Currxg()
    {
        $Classes = M('CCourse');

        if (IS_AJAX) {
            $Classes->create();
            $updata = $Classes->save(); // 根据条件保存修改的数据
            if ($updata) {

                $data = 0;
            } else {

                $data = 1;
            }
            echo $data;
        } else {
            $telist = R('Pub/Teacher');
            $classs = R('Pub/Classs');
            $scheinfo = R('Pub/Sche');
            $kc = R('Pub/Kc');
            $this->assign('kc', $kc);
            $this->assign('bcbh', I('get.bcbh'));
            // var_dump($sch);
            $this->assign('sche', $scheinfo);
            $this->assign('scheinfo', $scheinfo);
            $this->assign('telist', $telist);
            $this->assign('classs', $classs);
            $map['id'] = I('get.id');
            $us = $Classes->where($map)->find();
            $this->assign('us', $us);
            $this->display();
        }
    }

    public function del()
    {
        $id = I('get.id');
        $map['id'] = $id;
        if (I('get.lb') == 'classes') {
            $Classes = M('CSchedule');

            $del = $Classes->where($map)->delete();
            if ($del) {

                $this->success('删除成功');
            } else {

                $this->error('删除失败');
            }
        } elseif (I('get.lb') == 'curr') {
            $student = M('CCourse');

            $del = $student->where($map)->delete();
            if ($del) {

                $this->success('删除成功');
            } else {

                $this->error('删除失败');
            }
        } elseif (I('get.lb') == 'classes') {
            $student = D('CSchedule');

            $del = $student->where($map)->delete();
            if ($del) {

                $this->success('删除成功');
            } else {

                $this->error('删除失败');
            }
        }
    }

    public function showlist()
    {
        if (IS_AJAX) {
            $map['courseid'] = "测试";
            $kc = M('CSchedule')->where($map)->select();
            $this->assign('kc', $kc);
            $this->display();
        }
    }

    public function showlista()
    {
        if (IS_AJAX) {
            $map['schename'] = I('post.kcname');
            $scheid = M('CSchedule')->where($map)->getField('scheid');
            $mas['courseid'] = $scheid;
            $kc = M('CCourse')->where($mas)->select();
            // var_dump($courseid);
            $mab['scheid'] = $scheid;
            $scheid = M('CSchedule')->where($mab)->find();
            $this->assign('sche', $scheid);
            $this->assign('kc', $kc);
            $this->display();
        }
    }

    public function showbc()
    {
        if (I('post.kcname')) {
            $map['schename'] = array(
                'like',
                '%' . I('post.kcname') . '%'
            );
            $map['scheid'] = array(
                'like',
                '%' . I('post.kcname') . '%'
            );
            $map['_logic'] = 'OR';
            $list = M('CSchedule')->where($map)
                ->field('schename,scheid')
                ->select();;
            $this->ajaxReturn($list, 'JSON');
        }
    }

    public function delcurr()
    {
        if (IS_AJAX) {
            $curr = M('CCourse');
            $map['id'] = I('post.id');
            $del = $curr->where($map)->delete();
            if ($del) {

                $data = 0;
            } else {

                $data = 1;
            }
            echo $data;
        }
    }
    // put your code here
}

